home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16182 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: noc.netcom.net!news
  2. From: Sean Palmer <sean@delta.com>
  3. Newsgroups: comp.lang.c++,comp.lang.c,comp.os.msdos.programer,comp.os.ms-windows.programmer.misc
  4. Subject: Re: fastest code
  5. Date: Tue, 09 Apr 1996 15:28:22 -0400
  6. Organization: deltaComm Development, Inc.
  7. Message-ID: <316ABA56.3A68@delta.com>
  8. References: <316112A2.7D37@public.sta.net.cn> <31611AC9.7DE1@wight.hursley.ibm.com> <3162c21a.6084138@204.248.25.97> <31641DE2.31D4@halcyon.com> <31658942.99299605@204.248.25.97> <4k9g04$4ov@news1.mnsinc.com>
  9. NNTP-Posting-Host: landspeeder.delta.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 3.0B2 (Win95; I)
  14.  
  15. > : The original question was looking for this kind of information (even
  16. > : if the poster does not understand Big-O, which I don't know if he did
  17. > : or not).  He wants to know what compiler will best optimize the
  18. > : machine language code generated for a specific OS and computer
  19. > : architecture.
  20. > Optimizations have little to do with time complexities as well.  A
  21. > compiler today is not likely to change an O(n^2) algorithm into an
  22. > O(n) algorithm simply by optimizing the code, though an O(n) algorithm
  23. > might be optimized into an O(1) algorithm if you're talking about a
  24. > truly dumb programmer ;).
  25.  
  26. Being dumb aside, if the (fictitious bad) compiler, for this C++ code:
  27.  
  28. void test(int& x) {
  29.   if (!(x==0))
  30.     x=x+1;
  31.   }
  32.  
  33. generates the following x86 asm code:
  34.  
  35. test@i:
  36.   push esi
  37.   push edi
  38.   push ebp
  39.   mov ebp,esp
  40.   push eax
  41.   push ebx
  42.  
  43.   mov eax,[x]
  44.   mov ebx,[eax]
  45.   cmp ebx,0
  46.   jz @@loc2
  47.   jmp @@loc1
  48. @@loc2:
  49.   mov eax,[x]
  50.   mov ebx,[eax]
  51.   add ebx,1
  52.   mov eax,[x]
  53.   mov [x],ebx
  54. @@loc1:
  55.  
  56.   pop ebx
  57.   pop eax  
  58.   pop ebp
  59.   pop edi
  60.   pop esi
  61.   ret
  62.  
  63. while another compiler generates this code:
  64.  
  65. test@i:
  66.   mov ebx,[eax]
  67.   test ebx,ebx
  68.   jnz @@loc1
  69.   inc dword ptr [eax]
  70. @@loc1:
  71.   ret  
  72.  
  73.  
  74. which would you rather call in a 10000-iteration loop?
  75.  
  76. The difference, is that the first one would force you (if writing a 
  77. video game for instance) to code 30% of the game in assembly language 
  78. to get the necessary speed, where the second compiler would allow you to 
  79. code 90% of the game in good ol' readable, maintainable, portable C++...
  80. because writing in assembler wouldn't get you a noticeable speed gain.
  81.  
  82. Sure, a good optimizer isn't going to fix a bad algorithm. But a bad 
  83. optimizer will sure slow down a good algorithm... 
  84.  
  85. Take Borland vs. Watcom for instance. Borland has a hideous code 
  86. generator. Watcom's makes code that's often faster than what I would 
  87. write. It looks wierd, but it's fast. 
  88.  
  89. Borland won't even inline functions that contain loops or switch 
  90. statements. It even balks on complicated if statements. Watcom has no 
  91. such restriction.
  92.